import 'dart:developer';

import 'package:contained_tab_bar_view/contained_tab_bar_view.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:guochaoapp/common/constant/order_constant.dart';
import 'package:guochaoapp/controller/mine/mine_controller.dart';
import 'package:guochaoapp/model/order_list_entity.dart';
import 'package:guochaoapp/r.dart';

class GCMinePage extends StatefulWidget {
  @override
  _GCMinePageState createState() => _GCMinePageState();
}

class _GCMinePageState extends State<GCMinePage> {

  GCMineController c = Get.put(GCMineController());

  @override
  void initState() {
    super.initState();

    c.requestUserInfo();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(body: _initSubviews());
  }

  _initSubviews() {
    return Column(
      children: [
        Container(
          height: 176,
          child: Stack(children: [
            Image.asset(R.assetsIconMineBg,
                fit: BoxFit.cover, width: double.infinity),
            Obx(() => _headInfoView()),
          ]),
        ),
        Expanded(child: Obx(() => _tabView()))
      ],
    );
  }

  _headInfoView() {
    return Container(
      height: 176,
      child: Column(
        children: [
          SizedBox(height: 60),
          Row(
            children: [
              SizedBox(width: 26),
              Text(c.account.value,
                  style: TextStyle(
                      color: Colors.white,
                      fontSize: ScreenUtil().setSp(18),
                      fontWeight: FontWeight.bold)),
              Expanded(child: SizedBox()),
              GestureDetector(
                onTap: c.settingTap,
                child: Image.asset(R.assetsIconSettings, width: 22, height: 22),
              ),
              SizedBox(width: 15),
            ],
          ),
          Row(
            children: [
              SizedBox(width: 26),
              Text("手机号:" + c.phone.value,
                  style: TextStyle(
                    color: Colors.white,
                    fontSize: ScreenUtil().setSp(12),
                  )),
            ],
          ),
          SizedBox(height: 23),
          Row(
            children: [
              SizedBox(width: 26),
              Text("我的余额",
                  style: TextStyle(
                    color: Colors.white,
                    fontSize: ScreenUtil().setSp(14),
                  )),
              SizedBox(width: 15),
              Text(c.balance.value.toString(),
                  style: TextStyle(
                      color: Colors.white,
                      fontSize: ScreenUtil().setSp(21),
                      fontWeight: FontWeight.bold)),
              Expanded(child: SizedBox()),
              // GestureDetector(
              //   onTap: c.detailTap,
              //   child: Container(
              //     child: Row(
              //       children: [
              //         Text("查看详情  ",
              //             style: TextStyle(
              //               color: Color(0xFFBBC9DA),
              //               fontSize: ScreenUtil().setSp(10),
              //             )),
              //         Icon(
              //           Icons.arrow_forward_ios,
              //           color: Colors.white,
              //           size: ScreenUtil().setSp(12),
              //         )
              //       ],
              //     ),
              //   ),
              // ),
              SizedBox(width: ScreenUtil().setSp(15)),
            ],
          ),
        ],
      ),
    );
  }

  _tabView() {
    return ContainedTabBarView(
      tabBarProperties: TabBarProperties(
        height: ScreenUtil().setSp(50),
        indicatorColor: Theme.of(Get.context).primaryColor,
        indicatorPadding: EdgeInsets.only(
            left: ScreenUtil().setSp(55), right: ScreenUtil().setSp(55)),
        indicatorWeight: 2,
        labelColor: Theme.of(Get.context).primaryColor,
        unselectedLabelColor: Color(0x77000000),
      ),
      tabs: [
        Container(
          child: Row(mainAxisAlignment: MainAxisAlignment.center, children: [
            Image.asset(R.assetsIconDoingOrder,
                width: ScreenUtil().setSp(22), height: ScreenUtil().setSp(22)),
            SizedBox(width: ScreenUtil().setSp(5)),
            Text("处理中", style: TextStyle(fontSize: ScreenUtil().setSp(16))),
          ]),
        ),
        Container(
          child: Row(mainAxisAlignment: MainAxisAlignment.center, children: [
            Image.asset(R.assetsIconDoneOrder,
                width: ScreenUtil().setSp(22), height: ScreenUtil().setSp(22)),
            SizedBox(width: ScreenUtil().setSp(5)),
            Text("已完成", style: TextStyle(fontSize: ScreenUtil().setSp(16))),
          ]),
        ),
      ],
      views: [
        EasyRefresh.custom(
          emptyWidget: c.records.length == 0 ? _noDataView() : null,
          firstRefresh: true,
          enableControlFinishLoad: true,
          enableControlFinishRefresh: true,
          controller: c.er_controller,
          header: BallPulseHeader(color: Theme.of(Get.context).primaryColor),
          footer: BallPulseFooter(color: Theme.of(Get.context).primaryColor),
          onRefresh: () async {
            c.onRefresh(false);
          },
          onLoad: () async {
            c.onLoadMore(false);
          },
          slivers: <Widget>[
            SliverList(
              delegate: SliverChildBuilderDelegate((context, index) {
                return _recordItem(index);
              }, childCount: c.size),
            )
          ],
        ),
        EasyRefresh.custom(
          emptyWidget: c.records.length == 0 ? _noDataView() : null,
          firstRefresh: true,
          enableControlFinishLoad: true,
          enableControlFinishRefresh: true,
          controller: c.er_controller,
          header: BallPulseHeader(color: Theme.of(Get.context).primaryColor),
          footer: BallPulseFooter(color: Theme.of(Get.context).primaryColor),
          onRefresh: () async {
            c.onRefresh(true);
          },
          onLoad: () async {
            c.onLoadMore(true);
          },
          slivers: <Widget>[
            SliverList(
              delegate: SliverChildBuilderDelegate((context, index) {
                return _recordItem(index);
              }, childCount: c.size),
            )
          ],
        ),
      ],
      onChange: (index) => {log(index.toString())},
    );
  }

  _recordItem(int index) {
    OrderListDataRecord model = c.records[index];
    return GestureDetector(
        onTap: () => c.jumpToDetail(model.mallOrderNo, model.mallOrderStatus),
        child: Container(
            margin: EdgeInsets.only(
                top: ScreenUtil().setSp(5),
                left: ScreenUtil().setSp(8),
                right: ScreenUtil().setSp(8),
            ),
            decoration: BoxDecoration(
              color: Colors.white,
              borderRadius: BorderRadius.circular(8.0),
            ),
//            height: ScreenUtil().setSp(130),
            child: Column(
              children: [
                Container(
                  margin: EdgeInsets.only(
                      top: ScreenUtil().setSp(13),
                      left: ScreenUtil().setSp(15),
                      right: 10,
                    bottom: ScreenUtil().setSp(5),
                  ),
                  child: Column(
                    children: [
                      Row(
                        children: [
                          Image.asset(R.assetsIconSettleList,
                              height: ScreenUtil().setSp(18),
                              width: ScreenUtil().setSp(18)),
                          SizedBox(width: 6),
                          Text("订单号: ${model.mallOrderNo}",
                              style: TextStyle(
                                  fontSize: ScreenUtil().setSp(14),
                                  fontWeight: FontWeight.bold))
                        ],
                      ),
                      SizedBox(height: ScreenUtil().setSp(6)),
                      Row(
                        children: [
                          Column(
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: [
                              Row(
                                children: [
                                  Opacity(
                                      opacity: 0.5,
                                      child: Text("共计${model.numTotal}件商品",
                                          style: TextStyle(
                                              fontSize: ScreenUtil().setSp(12),
                                              color: Color(0xFF333333)))),
                                  SizedBox(width: ScreenUtil().setSp(16)),
                                  Opacity(
                                      opacity: 0.5,
                                      child: Text("总支付",
                                          style: TextStyle(
                                              fontSize: ScreenUtil().setSp(12),
                                              color: Color(0xFF333333)))),
                                  Text("￥${model.paymentPrice}",
                                      style: TextStyle(
                                          fontSize: ScreenUtil().setSp(12),
                                          color: Colors.red)),
                                ],
                              ),
                              SizedBox(height: ScreenUtil().setSp(4)),
                              Row(
                                children: [
                                  Image.asset(R.assetsIconSettleAddress,
                                      height: ScreenUtil().setSp(12),
                                      width: ScreenUtil().setSp(12)),
                                  Opacity(
                                    opacity: 0.5,
                                    child: Container(
                                        width: 290,
                                        child: Text(
                                            "收货地址: ${model.receiverAddr}",
                                            maxLines: 1,
                                            overflow: TextOverflow.ellipsis,
                                            style: TextStyle(
                                                fontSize:
                                                    ScreenUtil().setSp(12),
                                                color: Color(0xFF333333)))),
                                  ),
                                ],
                              )
                            ],
                          ),
                          Expanded(child: SizedBox()),
                          Image.asset(R.assetsIconArrowRight,
                              height: 18, width: 18),
                        ],
                      ),
                      SizedBox(height: ScreenUtil().setSp(8)),
                      Opacity(
                          opacity: 0.5,
                          child: Divider(
                            height: ScreenUtil().setSp(1),
                            color: Color(0xFF979797),
                          )),
                      SizedBox(height: ScreenUtil().setSp(8)),
                      Row(
                        children: [
                          Opacity(
                              opacity: 0.5,
                              child: Text("${model.createAt}",
                                  style: TextStyle(
                                      fontSize: ScreenUtil().setSp(10),
                                      color: Color(0xFF333333)))),
                          Expanded(child: SizedBox()),
                          _getStatus(model.mallOrderStatus)
                        ],
                      )
                    ],
                  ),
                ),
              ],
            )));
  }

  _noDataView() {
    return Column(
      mainAxisAlignment: MainAxisAlignment.center,
      children: [
        Container(
          height: 116,
          width: 108,
          child: Image.asset(R.assetsIconNoOrder),
        )
      ],
    );
  }

  _getStatus(String orderStatus) {
    Color color = Color(0xFF40A8B3);
    if (orderStatus == OrderConstant.statusInit) {
      color = Colors.red;
    }
    return Text(OrderConstant.orderStatus[orderStatus],
        style: TextStyle(color: color, fontSize: ScreenUtil().setSp(12)));
  }
}
